package com.lzq.leetcode.hoot100;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class s347 {

    public int[] topKFrequent(int[] nums, int k) {

        Map<Integer, Integer> map = new HashMap<>();

        for (int i=0; i<nums.length; i++) {
            map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
        }
        // 添加到优先权队列
        PriorityQueue<Node> pq = new PriorityQueue<>((n1, n2) -> {
            return n2.p - n1.p;
        });

        for (HashMap.Entry<Integer, Integer> e : map.entrySet()) {
            pq.add(new Node(e.getKey(), e.getValue()));
        }

        int[] ret = new int[k];

        for (int i=0; i<k; i++) {
            ret[i] = pq.poll().nums;
        }

        return ret;
    }

    class Node {
        int nums;
        int p;

        Node(int nums, int p) {
            this.nums = nums;
            this.p = p;
        }
    }

    public static void main(String[] args) {
        s347 s347 = new s347();
        System.out.println(s347.topKFrequent(new int[]{1, 1, 1, 2, 2, 3}, 2));
    }
}
